<template>
  <view class="promotion-container">
    <!-- 科技感背景层 -->
    <view class="tech-bg">
      <view class="tech-grid"></view>
      <view class="tech-glow"></view>
    </view>
    
    <view v-if="selecttype == 0 && (userInfo.is_promotion == 2)" class="main-content">
      <!-- 顶部欢迎区域 -->
      <view class="welcome-section">
        <view class="welcome-text">
          <text v-if="userInfo && userInfo.nickname">
            {{$t('daili.welcome')}}，<text class="highlight-text">{{userInfo.nickname}}</text>
          </text>
          <text v-else-if="userInfo && userInfo.username">
            {{$t('daili.welcome')}}，<text class="highlight-text">{{userInfo.username}}</text>
          </text>
          <text v-else>
            {{$t('daili.welcomeUse')}}
          </text>
        </view>
        <view class="promotion-tips">
          <text class="iconfont icon-promotion"></text>
          推广优质内容，获取丰厚收益
        </view>
      </view>

      <!-- 热门推广内容 -->
      <view class="promotion-section" v-if="hotdata[1]">
        <view class="section-header">
          <view class="section-title">
            <text class="title-text">热门推广</text>
            <view class="title-line"></view>
          </view>
          <view @click="huanyihuan()" class="refresh-btn">
            <text class="iconfont icon-refresh"></text>
            换一批
          </view>
        </view>
        
        <view class="promotion-grid">
          <view 
            v-for="(value,key) in hotdata" 
            :key="key" 
            v-if="key >= hyh && key < hyh + 6"
            class="promotion-card"
            @click="goToPromotion(value.id)"
          >
            <view class="hot-tag">热门</view>
            <view class="image-container">
              <image class="promotion-image" mode="aspectFill" :src="value.img"></image>
            </view>
            <view class="promotion-info">
              <view class="promotion-title">{{value.name}}</view>
              <view class="promotion-btn">立即推广</view>
            </view>
          </view>
        </view>
      </view>

      <!-- 热剧排行榜 -->
      <view class="ranking-section" v-if="givedata[3]">
        <view class="section-header">
          <view class="section-title">
            <text class="title-text">热剧排行榜</text>
            <view class="title-line"></view>
          </view>
        </view>
        
        <view class="ranking-list">
          <view 
            v-for="(value,key) in givedata" 
            :key="key" 
            v-if="key < 6"
            class="ranking-card"
            @click="goToPromotionDetail(value.id)"
          >
            <view class="rank-badge">TOP{{key + 1}}</view>
            <view class="image-container">
              <image class="ranking-image" mode="aspectFill" :src="value.img"></image>
            </view>
            <view class="ranking-info">
              <view class="ranking-title">{{value.name}}</view>
              <view class="promotion-btn">立即推广</view>
            </view>
          </view>
        </view>
      </view>

      <!-- 更多推广内容 -->
      <view class="more-promotion">
        <home-default :datasa="datasa" @item-click="goToPromotion"></home-default>
      </view>
    </view>

    <!-- 非推广用户展示 -->
    <view v-else-if="selecttype == 0" class="center-info">
      <view class="info-container" v-if="userInfo && Object.keys(userInfo).length > 0">
        <view class="message-text">
          <text class="iconfont icon-info"></text>
          {{$t('daili.contactCustomer')}}
        </view>
        <button class="tech-button" @click="goToKefu">
          <text class="iconfont icon-service"></text>
          {{$t('daili.contactService')}}
        </button>
      </view>
      <view v-else class="message-text loading-text">
        <text class="iconfont icon-loading"></text>
        {{$t('daili.loading')}}
      </view>
    </view>
  </view>
</template>

<script>
import { languageMixin } from '@/common/mixins.js';

export default {
  mixins: [languageMixin],
  data(){
    return {
      hyh:0,
      datasa:[],
      listjq:[],
      listbz:[],
      showdyxx:true,
      dataconfig:[],
      banners:[],
      datainfo:[],
      dataindex:[],
      hotdata:[],
      givedata:[],
      userInfo:{},
      typetab:[
        {name:'推荐短剧',id:1},
        {name:'剧情介绍',id:2},
        {name:'剧情壁纸',id:3},
      ],
      selecttype:0,
      type:1,
    }
  },
  computed:{},
  onShareAppMessage(e){},
  onShareTimeline(e){},
  onLoad(e){
    this.getUserInfo()
  },
  onShow() {
    this.getList();
    this.getUserInfo();
  },
  methods:{
    async getUserInfo(){
      try {
        let data = {};
        data.token = uni.getStorageSync("userinfo").token;
        data.uid = uni.getStorageSync("userinfo").id;
        uni.request({
          url: this.configs.webUrl+'/api/user/index',
          data: data,
          success: res =>{
            if(res.data.code==1){
              let ionfo=res.data.data
              ionfo.isLogin=true
              this.userinfo=ionfo
              this.userInfo=ionfo
              uni.setStorage({key: 'userinfo', data: res.data.data})
              
              if(res.data.data.avatar){
                var str = res.data.data.avatar;
                if(str.indexOf("data:image") != -1){
                  this.avatar='';
                }else{
                  if(str.indexOf("http") != -1){
                    this.avatar=res.data.data.avatar;
                  }else{
                    this.avatar=this.configs.imgUrl+res.data.data.avatar;
                  }
                }
              }else{
                this.avatar='';
              }
              uni.setStorage({key: 'avatar', data: this.avatar})
            }else{
              uni.showToast({ title: res.data.msg,icon:"none" });
            }
          },
          fail: (data, code) => {
            console.log('获取用户信息失败:', data);
          }
        });	

        const user_info = await uni.getStorage({key: 'userinfo'});
        if (!this.userInfo || Object.keys(this.userInfo).length === 0) {
          this.userInfo = user_info.data;
        }
        
        return this.userInfo;
      } catch (error) {
        console.error('获取用户信息失败:', error);
        return null;
      }
    },
    changeIndex(index){
      if(this.typetab[index].id){
        this.type=this.typetab[index].id
      }
      this.selecttype = index;
    },
    huanyihuan(){
      if(this.hyh<12){
        this.hyh=this.hyh+6
      }
      if(this.hyh>=12){
        this.hyh=0
      }
    },
    getList() {
      let this_=this
      let data = {};
      uni.request({
        url: this.configs.webUrl+'/api/video/indexdata',
        data: data,
        success: data => {
          uni.setStorage({key: 'config', data: data.data.config})
          this.banners=data.data.config.banner
          this.listbz=data.data.listbz
          this.listjq=data.data.listjq
          this.datasa=data.data.new
          this.hotdata=data.data.hotdata
          this.givedata=data.data.givedata
        },
        fail: (data, code) => {}
      });
    },
    // 统一跳转到推广页面
    goToPromotion(id) {
      uni.navigateTo({
        url:'/pages/client/daili-detail?id='+id+'&mid=0'
      })
    },
    // 跳转到排行榜推广详情
    goToPromotionDetail(id) {
      uni.navigateTo({
        url:'/pages/client/daili-detail?id='+id+'&mid=0'
      })
    },
    goToKefu() {
      if (typeof plus !== 'undefined' && plus.runtime) {
        plus.runtime.openURL("https://kefu.com");
      } else {
        window.open("https://kefu.com", "_blank");
      }
    }
  },
}
</script>

<style>
/* 基础样式 */
page {
  background: transparent;
  height: 100%;
}

/* 主容器 */
.promotion-container {
  min-height: 100vh;
  padding-bottom: 120rpx;
  box-sizing: border-box;
  position: relative;
}

/* 科技感背景 */
.tech-bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
  z-index: -1;
  overflow: hidden;
}

.tech-grid {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: 
    linear-gradient(rgba(255, 204, 0, 0.1) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 204, 0, 0.1) 1px, transparent 1px);
  background-size: 40px 40px;
  opacity: 0.3;
}

.tech-glow {
  position: absolute;
  width: 300%;
  height: 300%;
  top: -100%;
  left: -100%;
  background: radial-gradient(circle, rgba(255, 204, 0, 0.1) 0%, transparent 70%);
  animation: rotate-glow 60s linear infinite;
}

@keyframes rotate-glow {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 主内容区域 */
.main-content {
  position: relative;
  z-index: 1;
  padding: 30rpx;
}

/* 欢迎区域 */
.welcome-section {
  padding: 30rpx;
  background: rgba(30, 30, 30, 0.8);
  border-radius: 20rpx;
  border: 1px solid rgba(255, 204, 0, 0.3);
  box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.5);
  margin-bottom: 40rpx;
}

.welcome-text {
  font-size: 36rpx;
  font-weight: 500;
  color: #fff;
  margin-bottom: 20rpx;
}

.highlight-text {
  color: #FFD600;
  font-weight: bold;
}

.promotion-tips {
  font-size: 26rpx;
  color: #FFD600;
  display: flex;
  align-items: center;
}

.promotion-tips .iconfont {
  margin-right: 10rpx;
  font-size: 32rpx;
}

/* 区块标题 */
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30rpx;
}

.section-title {
  display: flex;
  align-items: center;
}

.title-text {
  font-size: 32rpx;
  font-weight: bold;
  color: #FFD600;
  padding-right: 20rpx;
  background: linear-gradient(90deg, #FFD600, #FFA500);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.title-line {
  flex: 1;
  height: 2rpx;
  background: linear-gradient(90deg, rgba(255, 204, 0, 0.5), transparent);
}

.refresh-btn {
  font-size: 26rpx;
  color: #FFD600;
  display: flex;
  align-items: center;
}

.refresh-btn .iconfont {
  margin-right: 8rpx;
}

/* 推广卡片网格 - 重点修改部分 */
.promotion-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20rpx;
  margin-bottom: 40rpx;
}

.promotion-card {
  position: relative;
  border-radius: 16rpx;
  overflow: hidden;
  background: rgba(40, 40, 40, 0.8);
  border: 1px solid rgba(255, 204, 0, 0.2);
  box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.3);
  transition: all 0.3s;
}

.promotion-card:active {
  transform: scale(0.98);
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.2);
}

.image-container {
  width: 100%;
  height: 0;
  padding-bottom: 150%; /* 控制图片容器的高宽比 */
  position: relative;
  overflow: hidden;
}

.promotion-image, .ranking-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.promotion-info {
  padding: 20rpx;
}

.promotion-title {
  font-size: 28rpx;
  color: #fff;
  margin-bottom: 20rpx;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.hot-tag {
  position: absolute;
  top: 10rpx;
  right: 10rpx;
  background: linear-gradient(135deg, #FFD600, #FF8C00);
  color: #000;
  font-size: 22rpx;
  padding: 4rpx 12rpx;
  border-radius: 20rpx;
  font-weight: bold;
  z-index: 2;
}

/* 排行榜 - 重点修改部分 */
.ranking-list {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20rpx;
  margin-bottom: 40rpx;
}

.ranking-card {
  position: relative;
  border-radius: 16rpx;
  overflow: hidden;
  background: rgba(40, 40, 40, 0.8);
  border: 1px solid rgba(255, 204, 0, 0.3);
  box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.3);
}

.ranking-info {
  padding: 20rpx;
}

.ranking-title {
  font-size: 28rpx;
  color: #fff;
  margin-bottom: 20rpx;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.rank-badge {
  position: absolute;
  top: 10rpx;
  left: 10rpx;
  width: 80rpx;
  height: 40rpx;
  background: linear-gradient(135deg, #FFD600, #FF8C00);
  color: #000;
  font-size: 24rpx;
  font-weight: bold;
  border-radius: 20rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
}

/* 推广按钮 */
.promotion-btn {
  width: 100%;
  height: 60rpx;
  background: linear-gradient(90deg, #FFD600, #FFA500);
  border-radius: 30rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #000;
  font-size: 26rpx;
  font-weight: bold;
}

/* 更多推广内容 */
.more-promotion {
  margin-top: 40rpx;
}

/* 非推广用户展示 */
.center-info {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(10, 10, 20, 0.9);
  z-index: 100;
}

.info-container {
  padding: 60rpx;
  background: rgba(30, 30, 30, 0.9);
  border-radius: 20rpx;
  border: 1px solid rgba(255, 204, 0, 0.3);
  box-shadow: 0 20rpx 50rpx rgba(0, 0, 0, 0.5);
  text-align: center;
  max-width: 80%;
}

.message-text {
  font-size: 32rpx;
  color: #fff;
  margin-bottom: 40rpx;
  line-height: 1.6;
}

.loading-text {
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 科技感按钮 */
.tech-button {
  background: linear-gradient(90deg, #FFD600, #FFA500);
  color: #000;
  border: none;
  border-radius: 50rpx;
  padding: 20rpx 40rpx;
  font-size: 28rpx;
  font-weight: bold;
  box-shadow: 0 10rpx 20rpx rgba(255, 204, 0, 0.3);
  position: relative;
  overflow: hidden;
  transition: all 0.3s;
}

.tech-button::after {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: linear-gradient(
    to bottom right,
    rgba(255, 255, 255, 0) 45%,
    rgba(255, 255, 255, 0.3) 50%,
    rgba(255, 255, 255, 0) 55%
  );
  transform: rotate(30deg);
  transition: all 0.3s;
}

.tech-button:active {
  transform: scale(0.95);
  box-shadow: 0 5rpx 10rpx rgba(255, 204, 0, 0.3);
}

.tech-button:active::after {
  left: 100%;
}

/* 响应式调整 */
@media (max-width: 750px) {
  .promotion-grid,
  .ranking-list {
    grid-template-columns: 1fr;
  }
  
  .image-container {
    padding-bottom: 140%;
  }
  
  .promotion-container {
    padding-bottom: 140rpx;
  }
}

/* iOS安全区域适配 */
.safe-area-inset-bottom {
  padding-bottom: constant(safe-area-inset-bottom);
  padding-bottom: env(safe-area-inset-bottom);
}

.refresh-btn{
  background: rgba(212, 175, 55, 0.2);
  color: #fff;
  border: none;
  padding: 16rpx 32rpx;
  border-radius: 40rpx;
  cursor: pointer;
  font-size: 28rpx;
  transition: background 0.3s;
}

.promotion-btn{
  background: rgba(212, 175, 55, 0.2);
  color: #d4af37;
  border: 1px solid rgba(212, 175, 55, 0.3);
}
</style>